package cc.leap.service;

import cc.leap.constant.UserConstants;
import cc.leap.mapper.SysDictDataMapper;
import cc.leap.mapper.SysDictTypeMapper;
import cc.leap.model.PageQuery;
import cc.leap.model.TableDataInfo;
import cc.leap.model.domain.SysDictData;
import cc.leap.model.domain.SysDictType;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Tran;

import java.util.List;

import static cc.leap.model.domain.table.SysDictTypeTableDef.SYS_DICT_TYPE;

/**
 * SysDictTypeService
 *
 * @author leap
 * @date 2024/8/19
 */
@Component
public class SysDictTypeService extends ServiceImpl<SysDictTypeMapper, SysDictType> {
    @Inject
    SysDictDataMapper dictDataMapper;

    @Inject
    SysDictTypeMapper dictTypeMapper;

    /**
     * 构造查询条件
     *
     * @param dictTypeBo
     * @return QueryWrapper
     */

    private QueryWrapper buildQueryWrapper(SysDictType dictTypeBo) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SYS_DICT_TYPE.DICT_NAME, SYS_DICT_TYPE.DICT_TYPE, SYS_DICT_TYPE.DICT_ID, SYS_DICT_TYPE.STATUS, SYS_DICT_TYPE.REMARK, SYS_DICT_TYPE.CREATE_TIME)
                .and(SYS_DICT_TYPE.DICT_NAME.like(dictTypeBo.getDictName()))
                .and(SYS_DICT_TYPE.DICT_TYPE.eq(dictTypeBo.getDictType()))
                .and(SYS_DICT_TYPE.CREATE_TIME.between(dictTypeBo.getParams().get("beginTime"), dictTypeBo.getParams().get("endTime")))
                .orderBy(SYS_DICT_TYPE.TENANT_ID.asc(),SYS_DICT_TYPE.DICT_TYPE.asc());
        return queryWrapper;
    }

    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dict = dictDataMapper.selectDictDataByType(dictType);
        return dict;
    }

    public TableDataInfo<SysDictType> selectDictTypePage(SysDictType dictType) {
        QueryWrapper queryWrapper = buildQueryWrapper(dictType);
        Page<SysDictType> page = this.pageAs(PageQuery.build(), queryWrapper, SysDictType.class);
        return TableDataInfo.build(page);
    }

    public SysDictType selectDictTypeById(Long dictId) {
        return dictTypeMapper.selectDictTypeById(dictId);
    }

    public List<SysDictType> selectDictTypeAll() {
        return dictTypeMapper.selectDictTypeAll();
    }

    public boolean checkDictTypeUnique(SysDictType dict) {
        Long dictId = ObjectUtil.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        SysDictType dictType = dictTypeMapper.checkDictTypeUnique(dict.getDictType());
        if (ObjectUtil.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    public int insertDictType(SysDictType dict) {
        int row = dictTypeMapper.insertDictType(dict);

        return row;
    }

    @Tran
    public int updateDictType(SysDictType dict) {
        SysDictType oldDict = dictTypeMapper.selectDictTypeById(dict.getDictId());
        dictDataMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType());
        int row = dictTypeMapper.updateDictType(dict);

        return row;
    }
}
